<?php
	/**
	 * 获取任务信息
	 */
    class GetTaskInfo extends APIBase {
		
		public $tag = "GetTaskInfo";
		public $isLogin = true;
		
		public function before() {	
			$this->initCacheRedis();
			return true;
		}

		public function logic() {
			
			/* 总赢局数，总局数任务   */
			$rets = $this->data_redis->hMget( "hu:{$this->uid}", array('total_board', 'total_win', 'play_reward_record', 'win_reward_record') );

			$play_reward_count = (int)$rets['play_reward_record'];
			$win_reward_count = (int)$rets['win_reward_record'];
			$play_total = $rets['total_board'];
			$win_total = $rets['total_win'];
			$play_can_reward = 0;
			$win_can_reward = 0;
			
			//累计局数奖励
			for ($i = 0;  $i < sizeof(Game::$can_reward_play_times); $i++ )
			{
				//如果已经达到领奖条件，并且没有领过，则可以领奖
				if ( ($play_total >= Game::$can_reward_play_times[$i]) && ( ($play_reward_count & (1 << $i)) == 0) )
				{
					$play_can_reward |= (1 << $i);
				}
			}			
			
			
			//累计胜利局数奖励
			for ($j = 0;  $j < sizeof(Game::$can_reward_win_times); $j++ )
			{
				//如果已经达到领奖条件，并且没有领过，则可以领奖
				if ( ($win_total >= Game::$can_reward_win_times[$j]) && ( ($win_reward_count & (1 << $j)) == 0) )
				{
					$win_can_reward |= (1 << $j);
				}
			}
				
			$rets['play_reward_record'] = (int)$rets['play_reward_record'];
			$rets['win_reward_record'] = (int)$rets['win_reward_record'];
			$rets['can_reward_play_times'] = Game::$can_reward_play_times;
			$rets['play_times_reward_array'] = Game::$play_times_reward_array;
			$rets['can_reward_win_times'] = Game::$can_reward_win_times;
			$rets['win_times_reward_array'] = Game::$win_times_reward_array;
			$rets['play_can_reward'] = (int)$play_can_reward;
			$rets['win_can_reward'] = (int)$win_can_reward;
			$rets['total_board'] = (int)$rets['total_board'];
			$rets['total_win'] = (int)$rets['total_win'];
			
			/* 奖券任务  */
			$coin_task = $this->data_redis->hMget("hu:{$this->uid}",array("coin_task_complete_record", "isget_coin_task_reward_record"));
			$coin_task_complete_record = $coin_task['coin_task_complete_record'];
			$isget_coin_task_reward_record = (int)$coin_task['isget_coin_task_reward_record'];
			//获取任务的完成进度
			//$coin_task_complete_record = "1|2|50|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0";
			$task_pieces = explode("|", $coin_task_complete_record);
			$taskids = $this->cache_redis->lRange("lcointask:ids", 0, -1);
			$cointasks = array();
			foreach ($taskids as $tid) {
				$task = $this->cache_redis->hMGet("hcointask:{$tid}", array("id", "name", "task_reward_coin", "task_value"));
				$tid = (int)$tid;
				$task['id'] = $tid;
				$task['name'] = (string) $task['name'];
				$task['task_reward_coin'] = (int) $task['task_reward_coin'];
				$task['task_value'] = (int) $task['task_value'];
			
				//如果奖券任务完成度 >= 奖励任务的规定完成度，则任务完成
				if ( (int) $task_pieces[$tid -1] >= $task["task_value"] )
				{
			
					$task['task_complete_value'] = $task['task_value'];
			
					//任务完成，未领取
					if ( ($isget_coin_task_reward_record & (1 << $tid-1)) == 0)
					{
						$task['flag'] = 1;
					}
					//任务完成，已经领取
					else
					{
						$task['flag'] = 2;
					}
				}
				//否则任务没有完成,完成值等于 $task_pieces[$tid -1 ], $task['flag'] = 0;
				else
				{
					$task['task_complete_value'] = (int)$task_pieces[$tid - 1];
					$task['flag'] = 0;
				}
			
				$cointasks[] = $task;
								
			}
			$rets['cointasks'] = $cointasks;		
			$this->returnData($rets, 0, "get task reward succ.\n");
		}
		
    	public function after() {
    		$this->deinitCacheRedis();
			$this->deinitDataRedis();
		}
    }
?>